﻿--------------------------
-- out-source functions --
--------------------------

--[[function callServerFunction ( funcname, ... )
	local arg = { ... }
	if ( arg [ 1 ] ) then
		for key, value in next, arg do
			if ( type ( value ) == "number" ) then arg [ key ] = tostring ( value ) end
		end
	end
	-- If the serverside event handler is not in the same resource, replace 'resourceRoot' with the appropriate element
	triggerServerEvent ( "onClientCallsServerFunction", resourceRoot , funcname, unpack ( arg ) )
end]]

function callClientFunction ( funcname, ... ) 
	local arg = { ... } 
	if ( arg [ 1 ] ) then
		for key, value in next, arg do arg [ key ] = tonumber ( value ) or value end
	end
	loadstring ( "return "..funcname ) () ( unpack ( arg ) ) 
end
addEvent ( "onServerCallsClientFunction", true ) 
addEventHandler ( "onServerCallsClientFunction", resourceRoot, callClientFunction )

---------------
-- variables --
---------------

localWeapons = {} -- localWeapons [ weaponID ] = weapon

--latentChanges = {} -- latentChanges [ weaponID ] = { [ key ] = elementData }

---------------------
-- basic functions --
---------------------

--[[local colShape = createColSphere ( 0, 0, 0, 300 )
function isElementWithinRange ( theElement ) 
	if isElement ( theElement ) then
		return isElementWithinColShape ( theElement, colShape )
	end
	return false
end]]

function allWeapons ()
	return getElementsByType ( "weapon" )
end

function allWeaponDummies ()
	return getElementsByType ( "weaponDummy" )
end

function getWeaponByID ( weaponID )
	return localWeapons [ weaponID ]
end

function getWeaponFromDummy ( weaponDummy )
	return localWeapons [ getElementID ( weaponDummy ) ]
end

-------------------
-- main funtions --
-------------------

function clientCreateWeapon ( weaponDummy, updateAllData )
	local weaponID = getElementID ( weaponDummy )
	local weaponModel = getElementData ( weaponDummy, "weaponModel" )
	local x, y, z = unpack ( getElementData ( weaponDummy, "weaponPosition" ) )
	local rx, ry, rz = unpack ( getElementData ( weaponDummy, "weaponRotation" ) )
	
	local weapon = createWeapon ( weaponModel, x, y, z )
	setElementRotation ( weapon, rx, ry, rz )
	
	localWeapons [ weaponID ] = weapon
	setElementParent ( weapon, weaponDummy )
	
	if updateAllData then
		local weaponAlpha = getElementData ( weaponDummy, "weaponAlpha" )
		if weaponAlpha then
			setElementAlpha ( weapon, weaponAlpha )
		end
		local weaponOwner = getElementData ( weaponDummy, "weaponOwner" )
		if weaponOwner then
			setWeaponOwner ( weapon, weaponOwner )
		end
		local weaponState = getElementData ( weaponDummy, "weaponState" )
		if weaponState then
			setWeaponState ( weapon, weaponState )
		end
		local weaponAttachedTo = getElementData ( weaponDummy, "weaponAttachedTo" )
		if weaponAttachedTo then
			local ax, ay, az, arx, ary, arz = unpack ( getElementData ( weaponDummy, "weaponAttachedOffsets" ) )
			attachElements ( weapon, weaponAttachedTo, ax, ay, az, arx, ary, arz )
		end
	end
end

function clientCreateAllWeapons ()
	for k, weaponDummy in pairs ( allWeaponDummies () ) do
		clientCreateWeapon ( weaponDummy, true )
	end
end

function clientDestroyWeapon ( weaponID )
	--local weapon = localWeapons [ weaponID ]
	--destroyElement ( weapon )
	localWeapons [ weaponID ] = nil
end

function clientSetWeaponPosition ( weaponDummy, x, y, z )
	local weapon = getWeaponFromDummy ( weaponDummy )
	setElementPosition ( weapon, x, y, z )
end

function clientSetWeaponRotation ( weaponDummy, rx, ry, rz )
	local weapon = getWeaponFromDummy ( weaponDummy )
	setElementRotation ( weapon, rx, ry, rz )
end

function clientSetWeaponAlpha ( weaponDummy, theAlpha )
	local weapon = getWeaponFromDummy ( weaponDummy )
	setElementAlpha ( weaponDummy, theAlpha )
end

function clientAttachWeaponToElement ( weaponDummy, theElement, x, y, z, rx, ry, rz )
	local weapon = getWeaponFromDummy ( weaponDummy )
	attachElements ( weapon, theElement, x, y, z, rx, ry, rz )
end

--[[function clientSetAttachedWeaponOffsets ( weaponDummy, x, y, z, rx, ry, rz ) -- clientAttachWeaponToElement used instead (look up)
	local weapon = getWeaponFromDummy ( weaponDummy )
	setElementAttachedOffsets 
end]]

function clientDetachWeapon ( weaponDummy )
	local weapon = getWeaponFromDummy ( weaponDummy )
	detachElement ( weapon )
end

function clientFireWeapon ( weaponDummy )
	local weapon = getWeaponFromDummy ( weaponDummy )
	fireWeapon ( weapon )
end

function clientSetWeaponModel ( weaponDummy, theModel )
	local weaponID = getElementID
	local weapon = getWeaponFromID ( weaponID )
	destroyElement ( weapon )
	clientCreateWeapon ( weaponDummy, true )
end

function clientSetWeaponState ( weaponDummy, theState )
	local weapon = getWeaponFromDummy ( weaponDummy )
	setWeaponState ( weapon, theState )
end

function clientSetWeaponOwner ( weaponDummy, theOwner )
	local weapon = getWeaponFromDummy ( weaponDummy )
	setWeaponOwner ( weapon, theOwner )
end